import React from "react";
import { inject, observer } from "mobx-react";
import FormDesigner from "./form";
import PathDesigner from './path';
import SettingDesigner from './setting';
import AppDesignerHeader from "./header";
import EdcExcel from "./excel";
import AnalysisDesigner from "./analysis";
import EdcData from "./data";
import renderNoRight from "../../utils/renderNoRight";
import InnerCreateDialog from "../dialog/InnerCreateDialog";

@inject('edcAppDesignerStore', 'edcAppFormStore')
@observer
export default class AppDesigner extends React.Component {
  constructor(props) {
    super();
    this.store = props.edcAppDesignerStore;
    this.state = {
      newVisible: false,
      displayType: '',
      fillinType: ''
    }
  }
  componentDidMount() {
    const { appid } = this.props.params;
    if (appid === '-2') {
      this.newMutilApp();
    } else if (appid === '-1') {
      this.newFreeApp();
    } else {
      this.store.initDesigner(this.props.params);
    }
  }
  componentWillReceiveProps(nextProps) {
    if ((this.props.params.appid === '-1' || this.props.params.appid === '-2') && parseInt(nextProps.params.appid) > 0) {
      this.store.initDesigner(nextProps.params);
    }
  }
  newMutilApp = () => {
    this.setState({
      newVisible: true,
      displayType: 'EXCEL',
      fillinType: 'MULTI'
    })
  }
  newFreeApp = () => {
    this.setState({
      newVisible: true,
      displayType: 'FORM',
      fillinType: 'FREE'
    })
  }
  render() {
    if (this.store.noRight) {
      return renderNoRight();
    }
    return (
      <div className='wea-edc-container wea-edc-app-designer'>
        <AppDesignerHeader ecId={`${this && this.props && this.props.ecId || ''}_AppDesignerHeader@mvjbjz`}
          store={this.store}
        />
        <div className='wea-edc-content'>
          {this.getContent()}
        </div>
        <InnerCreateDialog ecId={`${this && this.props && this.props.ecId || ''}_InnerCreateDialog@7mm1my`} router={this.props.router} {...this.state} visible={this.state.newVisible} onVisibleChange={this.visibleChange} onCancel={()=>window.close()}></InnerCreateDialog>
      </div>
    );
  }
  visibleChange = (visible) => {
    this.setState({ newVisible: visible });
  }
  getContent = () => {
    const { tabKey, readonly } = this.store;
    const { params } = this.props;

    return tabKey === 'form' ? <FormDesigner ecId={`${this && this.props && this.props.ecId || ''}_FormDesigner@omvx7h`} params={params} readonly={readonly} /> :
      tabKey === 'path' ? <PathDesigner ecId={`${this && this.props && this.props.ecId || ''}_PathDesigner@d4h3el`} params={params} readonly={readonly} /> :
        tabKey === 'excel' ? <EdcExcel ecId={`${this && this.props && this.props.ecId || ''}_EdcExcel@6yxghv`} params={params} readonly={readonly} /> :
          tabKey === 'analysis' ? <AnalysisDesigner ecId={`${this && this.props && this.props.ecId || ''}_AnalysisDesigner@soupco`} params={params} readonly={readonly} /> : tabKey === 'data' ?
            <EdcData ecId={`${this && this.props && this.props.ecId || ''}_EdcData@s4wdfb`} params={params} readonly={readonly}></EdcData> :
            tabKey === 'setting' ? <SettingDesigner ecId={`${this && this.props && this.props.ecId || ''}_SettingDesigner@3t3cf5`} params={params} readonly={readonly} /> : null;
  }
}